/**
 * @(#) ReqWorkTimeProxy.as
 *
 * @date		2009. 03. 10.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	 근태신청 Proxy - Proxy
 * 				근태신청 리스트를 조회하고 저장, 수정, 삭제한다.
 */
package  com.skt.common_hr.teamworktime.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.teamworktime.TeamWorkTimeFacade;
	import com.skt.common_hr.teamworktime.model.vo.ReqVacationListVO;
	import com.skt.common_hr.teamworktime.model.vo.ReqVacationVO;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.collections.ArrayCollection;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
		
	public class ReqWorkTimeProxy extends CommonProxy
	{
		public static const NAME:String = "ReqWorkTimeProxy";
		public var reqVacationList:ArrayCollection;
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function ReqWorkTimeProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function InsertWorkTime(type:String, table:Array):void
		{
			var param:Object = new Object();
			param["I_PROC_TYPE"] 	= "I";				//처리구분(I:신규,M:수정)
			param["I_WSTATUS"]		= type;				//결재유형(1:임시저장,2:승인요청)
			var tables:Object = new Object();
			tables["ITAB"] = table;
			
			saveData("Z_HR_0022", param, tables, resultInsert, faultInsert);
		}



		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function modifyWorkTime(type:String, table:Array):void
		{
			var param:Object = new Object();
			param["I_PROC_TYPE"] 	= "M";				//처리구분(I:신규,M:수정)
			param["I_WSTATUS"]		= type;			//결재유형(1:임시저장,2:승인요청)
			var tables:Object = new Object();
			tables["ITAB"] = table;

			saveData("Z_HR_0022", param, tables, resultInsert, faultInsert);
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function resultLoad(event:ResultEvent):void
		{
			if(retCode==0) {  
				reqVacationList = parseVacationList(event.result.IOTAB);
				facade.sendNotification(TeamWorkTimeFacade.LOAD_REQ_VACATION_COMPLETE, this);
			}
			else {
				trace( "failed to ReqWorkTimeProxy: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(TeamWorkTimeFacade.LOAD_REQ_VACATION_FAILED, this);
			}
		}
		
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function faultLoad(event:FaultEvent):void
		{
			trace("Fault message in ReqWorkTimeProxy: " + event.fault.faultString);
			facade.sendNotification(TeamWorkTimeFacade.LOAD_REQ_VACATION_FAILED, this);
		}
		
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function resultInsert(event:ResultEvent):void
		{
			if(retCode==0) {  
				//vacationList = parseVacationList(event.result.CALTAB2);
				facade.sendNotification(TeamWorkTimeFacade.INSERT_REQ_WORKTIME_COMPLETE, this);
			}
			else {
				trace( "failed to ReqWorkTimeProxy: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(TeamWorkTimeFacade.INSERT_REQ_WORKTIME_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function faultInsert(event:FaultEvent):void
		{
			trace("Fault message in ReqWorkTimeProxy: " + event.fault.faultString);
			facade.sendNotification(TeamWorkTimeFacade.INSERT_REQ_WORKTIME_FAILED, this);
			CursorManager.removeBusyCursor();
		}
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 제증명서 종류 Code list
		 */
		private function parseVacationList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:ReqVacationListVO = new ReqVacationListVO();
				vo.PERNR 		= rec["PERNR"];
				vo.ENAME		= rec["ENAME"];
				vo.DGBN			= rec["DGBN"];
				vo.DGBNT		= rec["DGBNT"];
				vo.AWART		= rec["AWART"];
				vo.AWATX		= rec["AWATX"];
				vo.BEGDA		= rec["BEGDA"];
				vo.ENDDA		= rec["ENDDA"];
				vo.BEGUZ		= rec["BEGUZ"];
				vo.ENDUZ		= rec["ENDUZ"];
				vo.TIMES		= rec["TIMES"];
				vo.IPERNR		= rec["IPERNR"];
				vo.INAME		= rec["INAME"];
				vo.WPERNR		= rec["WPERNR"];
				vo.WSTATUS		= rec["WSTATUS"];
				vo.WSTATX		= rec["WSTATX"];
				vo.TABNR		= rec["TABNR"];
				vo.TEXT			= rec["TEXT1"];
				retArr.addItem(vo);
			}
			return retArr;	
		}
		
	}
}